package com.bdqn.dao.Impl;

import com.bdqn.dao.CheckWorkDao;
import com.bdqn.entity.SpecialWay;
import com.bdqn.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Date;

public class CheckWorkDaoImpl implements CheckWorkDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public int findByIdAndDate(String empId, String date) {
        int i = 0;
        try {
            String sql = "select count(*) from checkwork where empId=? and date=?";
            i=template.queryForObject(sql,Integer.class,empId,date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public boolean insertIntoEnd(String time,String date,String empId) {
        String sql = "update checkwork set endtime=? where empId=? and date=? ";
        return template.update(sql,time,empId,date)>0;
    }

    @Override
    public boolean insertIntostart(String time, String date, String empId) {
        String sql = "insert into checkwork(empId,date,starttime,latecome,absenteeism,leave_early) values(?,?,?,?,?,?) ";
        return template.update(sql,empId,date,time,0,0,0)>0;
    }
    @Override
    public int addEmpl(String empId, String name, String depId, String password) {
        String sql = "insert into employee(empId,password,name,depId,permission) values(?,?,?,?,?) ";
        return template.update(sql,empId,password,name,depId,0);
    }


    @Override
    public Date findstart(Integer depId, int week) {
        String sql = "SELECT start from `group` as g ,`work` as w WHERE g.workId = w.workId and g.depId=? and weekday=?";
        return template.queryForObject(sql,Date.class,depId,week);
    }

    @Override
    public Date findEnd(Integer depId, int week) {
        String sql = "SELECT end from `group` as g ,`work` as w WHERE g.workId = w.workId and g.depId=? and weekday=?";
        return template.queryForObject(sql,Date.class,depId,week);
    }

    @Override
    public int FindWeek(Integer depId, int week) {
        int count = 0;
        String sql = "SELECT count(*) from `group` as g ,`work` as w WHERE g.workId = w.workId and g.depId=? and weekday=?";
        try {
            count = template.queryForObject(sql,Integer.class,depId,week);
        } catch (Exception e) {

        }
        return count;
    }

    @Override
    public int updateleaveEarly_true(String empId, String date) {
        String sql = "update checkwork set leave_early=? where empId=? and date=? ";
        return template.update(sql,0,empId,date);
    }

    @Override
    public SpecialWay findByDate(String date,int depId) {
        SpecialWay specialWay = null;
        String sql = "select specialdate,status,start,end from specialdate where specialdate=? and depId=?";
        try {
            specialWay = template.queryForObject(sql,new BeanPropertyRowMapper<SpecialWay>(SpecialWay.class),date,depId);
        } catch (Exception e) {

        }
        return specialWay;
    }

    @Override
    public int findWeek(Integer depId, int week) {
        int count = 0;
        String sql = "select count(*) from way where depId=? and week=? ";
        try {
            count = template.queryForObject(sql,Integer.class,depId,week);
        } catch (Exception e) {
        }
        return count;
    }

    @Override
    public int findSpecial(String date) {
        String sql = "select count(*) from specialdate where specialdate=?";
        return template.queryForObject(sql,Integer.class,date);
    }

    @Override
    public Date FindEndByIdAndDate(String empId, String date) {
        String sql = "select endtime from checkwork where date=? and empId=?";
        return template.queryForObject(sql,Date.class,date,empId);
    }

    @Override
    public Date findSway(int week, Integer depId) {
        Date date = null;
        String sql = "select start from way where week=? and depId=? ";
        try {
            date = template.queryForObject(sql,Date.class,week,depId);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return date;
    }

    @Override
    public Date findway(int week, Integer depId) {
        Date date = null;
        String sql = "select end from way where week=? and depId=? ";
        try {
            date = template.queryForObject(sql,Date.class,week,depId);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return date;
    }

    @Override
    public int updateleaveEarly(String empId, String date) {
        String sql = "update checkwork set leave_early=? where empId=? and date=? ";
        return template.update(sql,1,empId,date);
    }

    @Override
    public int updatelatecome(String empId,String date) {
        String sql = "update checkwork set latecome=? where empId=? and date=? ";
        return template.update(sql,1,empId,date);
    }

    @Override
    public Date FindByIdAndDate(String empId, String date) {
        String sql = "select starttime from checkwork where date=? and empId=?";
        return template.queryForObject(sql,Date.class,date,empId);
    }

    @Override
    public int deleteFromCheck(String empId) {
        String sql = "delete from checkwork where empId=?";
        return template.update(sql,empId);
    }

    @Override
    public int deleteByEmpId(String empId) {
        String sql = "delete from employee where empId=?";
        return template.update(sql,empId);
    }

    @Override
    public int findByEmpIdAndName(String empId, String name) {
        String sql = "select count(*) from employee where empId=? and name=?";
        return template.queryForObject(sql,Integer.class,empId,name);
    }

    @Override
    public int findByEmpId(String empId) {
        String sql = "select count(*) from employee where empId=?";
        return template.queryForObject(sql,Integer.class,empId);
    }
}
